package edu.pinkhub.shiro_test.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import edu.pinkhub.shiro_test.entity.User;
import edu.pinkhub.shiro_test.service.UserService;
import edu.pinkhub.shiro_test.util.DigestsUtil;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Random;

@RequestMapping("/user/")
@Controller
public class UserControlller {
    @Autowired
    public UserService userService;
    @RequestMapping("toRegister")
    public String toRegister(){
        return "register";
    }
    @PostMapping("register")
    public String register(HttpServletRequest request, Model model){
        String account = request.getParameter("account");
        String _password = request.getParameter("password");
        Map<String, String> info = DigestsUtil.entryptPassword(_password);
        String password = info.get("password");
        String salt = info.get("salt");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("account",account);
        User userDB = userService.getOne(wrapper);
        if(userDB!=null){
            model.addAttribute("msg","账号已存在");
            return "register";
        }else{
            User user = new User();
            user.setAccount(account);
            user.setPassword(password);
            user.setSalt(salt);
            user.setRealName("pinkhub");
            user.setStatus(1);
            Random random = new Random();
            user.setUserId(Integer.toString(random.nextInt(1000)));
            user.setTitle("普通用户");
            user.setPerms("query,update");
            userService.save(user);
            return "login";
        }
    }
    @PostMapping("login")
    public String login(String account,String password, Model model){
        UsernamePasswordToken token = new UsernamePasswordToken(account, password);
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(token);
            return "index";
        }catch(UnknownAccountException e){
            model.addAttribute("msg","账号不正确");
            return "login";
        }catch(IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }
    @RequestMapping("add")
    public String add(){
        return "/user/add";
    }
    @RequestMapping("edit")
    public String edit(){
        return "/user/edit";
    }
    @RequestMapping("noAuth")
    public String noAuth(){
        return "noAuth";
    }
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }
}
